PNB 2A03 - Python for PNB¶

Introduction to Python¶

David Feinberg

Logistics¶

Instructor¶

  • Dr. David R. Feinberg
  • Email: (feinberg@mcmaster.ca)
  • Pronouns: He/Him
  • Office: Room 407, Psychology Complex (PC)
  • Office Hours:
    • EVERY WEEKDAY (Except Holidays)
    • 10:30am-11:30am in PC 154 (computer cluster) for coding help
    • by appointment in PC 407 for other matters

Teaching Assistants¶

  • Yassaman Ommi (ommiy@mcmaster.ca)
  • Isaac Kinley (kinleyid@mcmaster.ca)
  • Office hours by appointment

Schedule/Location¶

  • Lectures: Mondays 12:30PM
    • 2:20PM BSB-B103
  • Labs: Thursdays
    • 3:30-4:20 BSB-108

Please wear a mask in class¶

Online Course Delivery¶

DO NOT ATTEND CLASS IN PERSON IF YOU ARE ILL

All lectures and labs will be streamed live via zoom and video recordings, audio recordings, and text transcripts of all lectures and labs will be posted online.¶

In the event that I am ill, and I will get ill since I have a child in daycare, class will be delivered live online.

There are different links for lectures and labs, so if you log on to one and nobody is there, check the other link. The links are on the next 2 slides. These are the same links for each lab/lecture whether I am delivering material from the classroom or my basement.

Lectures¶

David Feinberg is inviting you to a scheduled Zoom meeting.

Topic: PNB 2A03 Lectures Time: Jan 5, 2023 12:30 PM Eastern Time (US and Canada)

  • Every week on Thu, until Apr 20, 2023, 16 occurrence(s)
  • Jan 5, 2023 12:30 PM
  • Jan 12, 2023 12:30 PM
  • Jan 19, 2023 12:30 PM
  • Jan 26, 2023 12:30 PM
  • Feb 2, 2023 12:30 PM
  • Feb 9, 2023 12:30 PM
  • Feb 16, 2023 12:30 PM
  • Feb 23, 2023 12:30 PM
  • Mar 2, 2023 12:30 PM
  • Mar 9, 2023 12:30 PM
  • Mar 16, 2023 12:30 PM
  • Mar 23, 2023 12:30 PM
  • Mar 30, 2023 12:30 PM
  • Apr 6, 2023 12:30 PM
  • Apr 13, 2023 12:30 PM
  • Apr 20, 2023 12:30 PM

Please download and import the following iCalendar (.ics) files to your calendar system.
Weekly: https://mcmaster.zoom.us/meeting/tJYlf-2vqDMtG9N87fa6HRvzAI_7X_84kdvz/ics?icsToken=98tyKuCqpjMuHdKdtxiARowQGoqgLO3wplxajfpzxEjjAnZ7UBXsF8t9ZYpASIzb

Join Zoom Meeting
https://mcmaster.zoom.us/j/92884768717?pwd=c0pXMWRyb3dFS2Vvd0lCdjlvZFBqZz09

Meeting ID: 928 8476 8717
Passcode: 380562
One tap mobile

  • +14388097799,,92884768717#,,,,*380562# Canada
  • +15873281099,,92884768717#,,,,*380562# Canada

Dial by your location

  • +1 438 809 7799 Canada
  • +1 587 328 1099 Canada
  • +1 613 209 3054 Canada
  • +1 647 374 4685 Canada
  • +1 647 558 0588 Canada
  • +1 778 907 2071 Canada
  • +1 204 272 7920 Canada

Meeting ID: 928 8476 8717
Passcode: 380562
Find your local number: https://mcmaster.zoom.us/u/asGiNUVhB

Join by SIP
92884768717@zoomcrc.com

Join by H.323

  • 162.255.37.11 (US West)
  • 162.255.36.11 (US East)
  • 221.122.88.195 (China)
  • 115.114.131.7 (India Mumbai)
  • 115.114.115.7 (India Hyderabad)
  • 213.19.144.110 (Amsterdam Netherlands)
  • 213.244.140.110 (Germany)
  • 103.122.166.55 (Australia Sydney)
  • 103.122.167.55 (Australia Melbourne)
  • 209.9.211.110 (Hong Kong SAR)
  • 149.137.40.110 (Singapore)
  • 64.211.144.160 (Brazil)
  • 149.137.68.253 (Mexico)
  • 69.174.57.160 (Canada Toronto)
  • 65.39.152.160 (Canada Vancouver)
  • 207.226.132.110 (Japan Tokyo)
  • 149.137.24.110 (Japan Osaka)

Meeting ID: 928 8476 8717
Passcode: 380562

Labs¶

David Feinberg is inviting you to a scheduled Zoom meeting.

Topic: PNB 2A03 Labs Time: Jan 5, 2023 03:30 PM Eastern Time (US and Canada)

  • Every week on Thu, until Apr 20, 2023, 16 occurrence(s)
  • Jan 5, 2023 03:30 PM
  • Jan 12, 2023 03:30 PM
  • Jan 19, 2023 03:30 PM
  • Jan 26, 2023 03:30 PM
  • Feb 2, 2023 03:30 PM
  • Feb 9, 2023 03:30 PM
  • Feb 16, 2023 03:30 PM
  • Feb 23, 2023 03:30 PM
  • Mar 2, 2023 03:30 PM
  • Mar 9, 2023 03:30 PM
  • Mar 16, 2023 03:30 PM
  • Mar 23, 2023 03:30 PM
  • Mar 30, 2023 03:30 PM
  • Apr 6, 2023 03:30 PM
  • Apr 13, 2023 03:30 PM
  • Apr 20, 2023 03:30 PM Please download and import the following iCalendar (.ics) files to your calendar system.
    Weekly: https://mcmaster.zoom.us/meeting/tJEucOCpqzssGtchyZKiPCTmxKdQZLYlcuxg/ics?icsToken=98tyKuCtrTwjG9GVthmERowMA4_CXevwmGJdjbdelAbfKCljQTf7LslGA4hTKdTT

Join Zoom Meeting
https://mcmaster.zoom.us/j/95379150603?pwd=aTRjcTU4UW1SYnVYSjc3dCtwRHVSUT09

Meeting ID: 953 7915 0603
Passcode: 376220
One tap mobile

  • +16475580588,,95379150603#,,,,*376220# Canada
  • +17789072071,,95379150603#,,,,*376220# Canada
  • Dial by your location
  • +1 647 558 0588 Canada
  • +1 778 907 2071 Canada
  • +1 204 272 7920 Canada
  • +1 438 809 7799 Canada
  • +1 587 328 1099 Canada
  • +1 613 209 3054 Canada
  • +1 647 374 4685 Canada
    Meeting ID: 953 7915 0603
    Passcode: 376220
    Find your local number: https://mcmaster.zoom.us/u/aercCT425d

Join by SIP 95379150603@zoomcrc.com

Join by H.323

  • 162.255.37.11 (US West)
  • 162.255.36.11 (US East)
  • 221.122.88.195 (China)
  • 115.114.131.7 (India Mumbai)
  • 115.114.115.7 (India Hyderabad)
  • 213.19.144.110 (Amsterdam Netherlands)
  • 213.244.140.110 (Germany)
  • 103.122.166.55 (Australia Sydney)
  • 103.122.167.55 (Australia Melbourne)
  • 209.9.211.110 (Hong Kong SAR)
  • 149.137.40.110 (Singapore)
  • 64.211.144.160 (Brazil)
  • 149.137.68.253 (Mexico)
  • 69.174.57.160 (Canada Toronto)
  • 65.39.152.160 (Canada Vancouver)
  • 207.226.132.110 (Japan Tokyo)
  • 149.137.24.110 (Japan Osaka)

Meeting ID: 953 7915 0603
Passcode: 376220

Course Websites:¶

  • Avenue2Learn
  • https://drfeinberg.github.io/PNB-2A03/

Expectations & Approach¶

  • Goal: to learn practical programming in Python
  • How: hands-on, community driven, skills based course, assignment & project driven
  • Lectures & Lab Sections will be used for interactive activities
  • Assignments, coding labs & a final project will be designed to get you coding

Grading¶

  • Assignemtns - 30%
  • Labs - 35%
  • Final Project - 35%

Grading¶

  • Assignments are given in-class during Thursday Labs and are to be finished at home.
  • Assignments are due the Thursday after they are assigned (unless it's reading week, then you get an extra week)
  • You may NOT submit late assignments or labs or final projects. Unless granted an extension by an instructor prior to the due date, anything late is marked as a 0.
  • Assignment scores are averaged and equally weighted all assignments in the term. There are fewer assignments than there are labs.

Grading¶

  • Final Project 35% of the total grade
    • The final project is due on the last day of class.
    • Late projects will be marked as zero.

Why is computer programing being taught in PNB?¶

  • Programing is an extremely important skill for all sciences.
  • Teaching transferable skills that can be used in and out of academia
  • Producing reproducable science

Programing uses in PNB¶

How it's used¶

  • Stimuli creation
  • Data collection
  • Data processing
  • Statistical analysis

Why Python instead of another language?¶

  • Python is a high level scripting language
    • You can start doing complex things quickly
  • Relatively easy to learn
    • Programming skills can be transferred to any language
  • Many libraries available for science
    • Don't reinvent the wheel
  • Python ecosystem
    • A culture of sharing and helpfulness
  • Python is open-source and accessible
    • Open-source means anyone can look under the hood, see how things are made, fix them, copy them, or even repackage and sell off the parts for profit.

Why is it called Python?¶

  • It is named after British comedy group Monty Python

What does Python look like?¶

In [1]:
print("Hello, world!")
Hello, world!
In [2]:
a = 1
b = 2

c = a + b

print(c)
3
In [3]:
import math

math.sqrt(81)
Out[3]:
9.0

That's great, we can do math...¶

I can do that on my phone, so how is this usefull for PNB again?¶

In [4]:
import statistics

x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("Mean of x: ", statistics.mean(x))
print("Standard deviation of x: ", statistics.stdev(x))
print("Variance of x: ", statistics.variance(x))
Mean of x:  5
Standard deviation of x:  3.3166247903554
Variance of x:  11
In [5]:
from scipy.stats.stats import pearsonr

a = [1, 4, 6, 8, 10, 12]
b = [1, 2, 3, 4, 5, 2]   
r, p = pearsonr(a,b)
print("The correlation coefficient between a & b is r = {}".format(r))
print("The p-value of that correlation is p = {}".format(p))
if p < 0.05:
    print("The correlation is significant at p < 0.5")
elif p > 0.05:
    print("The correlation is not significant at p < 0.5")
The correlation coefficient between a & b is r = 0.5688448442065138
The p-value of that correlation is p = 0.23876740864265822
The correlation is not significant at p < 0.5
In [7]:
import matplotlib
import matplotlib.pyplot as plt
%matplotlib

plt.scatter(a, b)
plt.show()
Using matplotlib backend: Qt5Agg

YouTube_dl¶

  • A thesis student in my lab once analyzed the frequency components of different types of songs for her project
  • Python allowed us to download and analyze all of the songs from YouTube automatically, and then analyze the acoustic properties of the songs
  • Here is the example code...

Download the song¶

In [1]:
try: 
    import youtube_dl
except:
    !pip install youtube-dl
    import youtube_dl
import pandas as pd

song_list = [
    'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
]

ydl_opts = {
    'format': 'best',
    'outtmpl': 'my_favourite_song.webm'
}

for song in song_list:
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([song])
        
[youtube] dQw4w9WgXcQ: Downloading webpage
[youtube] dQw4w9WgXcQ: Downloading player e5f6cbd5
[download] Destination: my_favourite_song.webm
[download] 100% of 8.70MiB in 01:5979KiB/s ETA 00:001

Play the song¶

In [1]:
import io
import base64
from IPython.display import HTML

video = io.open('my_favourite_song.webm', 'r+b').read()
encoded = base64.b64encode(video)
HTML(data='''<video alt="test" controls>
                <source src="data:video/webm;base64,{0}" type="video/webm" />
             </video>'''.format(encoded.decode('ascii')))
Out[1]: